package com.lin.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lin.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.sound.midi.MetaEventListener;
import java.time.LocalDateTime;

/**
 * @auther lin
 * @create 2022-05-27 14:14
 * 自定义组件，公共字段的填充
 * ThreadLocal:不是一个Thread，而是Threa的一个局部变量，具有线程隔离的效果，
 *
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充【insert】....");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", new Long(1));
        //过滤器和update都属于同一个线程
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        log.info(metaObject.toString());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充【update】....");
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        log.info(metaObject.toString());
    }
}
